package morning.cat.study.commons;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.SerializationUtils;
import org.junit.Test;

public class SerializableTest {

	@Test
	public void test() throws IOException {

		User user = new User();
		user.setId(1001);
		user.setName("China");
		user.setDate(Calendar.getInstance().getTime());
		user.setStr("qia");
		System.out.println(user);

		// 序列化
		byte[] bytes = SerializationUtils.serialize(user);

		File file = new File("commons.bin");
		FileUtils.writeByteArrayToFile(file, bytes);
	}

	@Test
	public void test2() throws IOException {
		File file = new File("commons.bin");
		byte[] bytes = FileUtils.readFileToByteArray(file);

		// 反序列化
		User user2 = SerializationUtils.deserialize(bytes);
		System.out.println(user2);
	}

}
